package com.ccrfid.rmc6.service.video;

import com.ccrfid.rmc6.entity.sys.DictEntity;
import com.ccrfid.rmc6.entity.video.VideoCameraEntity;
import com.ccrfid.rmc6.entity.video.VideoOrgEntity;
import com.ccrfid.rmc6.entity.video.VideoPlatformEntity;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.pojo.hikartemis.resp.CameraInfo;
import com.ccrfid.rmc6.pojo.hikartemis.resp.RegionInfo;
import com.ccrfid.rmc6.pojo.open8200.resp.DataCamera;
import com.ccrfid.rmc6.pojo.open8200.resp.DataOrg;
import com.ccrfid.rmc6.repository.sys.DictRepository;
import com.ccrfid.rmc6.repository.video.VideoCameraRepository;
import com.ccrfid.rmc6.repository.video.VideoOrgRepository;
import com.ccrfid.rmc6.repository.video.VideoPlatformRepository;
import com.ccrfid.rmc6.ws.c5.videosync.VideoSyncClient;
import com.ccrfid.rmc6.ws.c5.videosync.pojo.ListVideoRequest;
import com.ccrfid.rmc6.ws.c5.videosync.pojo.VideoRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class VideoSyncServiceImpl implements VideoSyncService {
    private static final Logger logger = LoggerFactory.getLogger(VideoSyncServiceImpl.class);
    @Autowired
    private VideoSyncClient videoSyncClient;
    @Autowired
    private VideoOrgRepository videoOrgRepository;
    @Autowired
    private VideoCameraRepository videoCameraRepository;
    @Autowired
    private VideoPlatformRepository videoPlatformRepository;
    @Autowired
    private DictRepository dictRepository;
    @Autowired
    private HikArtemisService hikArtemisService;
    @Autowired
    private Open8200Service open8200Service;

    @Override
    public void syncVideoPlatform(VideoPlatformEntity platformEntity) {
        syncVideo(platformEntity, null);
    }

    @Override
    public void syncVideoOrg(VideoOrgEntity orgEntity) {
        if (orgEntity.getPlatformId() != null) {
            VideoPlatformEntity platformEntity = videoPlatformRepository.getById(orgEntity.getPlatformId());
            if (platformEntity != null) {
                syncVideo(platformEntity, orgEntity.getOrgSn());
            }
        }
    }

    @Override
    public Integer getIVersion(Integer platformVersion) {
        DictEntity dictEntity = dictRepository.getById(platformVersion);
        if (dictEntity != null) {
            return Integer.parseInt(dictEntity.getDictSn() != null ? dictEntity.getDictSn() : "0");
        } else {
            return 0;
        }
    }

    private void syncVideo(VideoPlatformEntity platformEntity, String orgCode) {
        if (platformEntity.getPlatformVersion() < Constants.VIDEO_VERSION.HK_ARTEMIS_B.id) {
            ListVideoRequest request = new ListVideoRequest();
            request.setiVersion(getIVersion(platformEntity.getPlatformVersion()));
            request.setcIp(platformEntity.getIp());
            request.setiPort(platformEntity.getPort());
            request.setcUsername(platformEntity.getUsername() == null ? "" : platformEntity.getUsername());
            request.setcPassword(platformEntity.getPasswd() == null ? "" : platformEntity.getPasswd());
            request.setcOrgCode(orgCode == null ? "" : orgCode);

            VideoRes[] resList = videoSyncClient.listVideo(platformEntity.getSyncServiceUrl(), request);
            if (resList != null && resList.length > 0) {
                for (VideoRes res : resList) {
                    updateVideoRes(res, platformEntity);
                }
            }
        } else if (platformEntity.getPlatformVersion().equals(Constants.VIDEO_VERSION.HK_ARTEMIS_B.id)
                || platformEntity.getPlatformVersion().equals(Constants.VIDEO_VERSION.HK_ARTEMIS_C.id)) {
            //海康Artemis Infovision SPCC平台同步
            syncHikArtemisVideo(platformEntity, orgCode);
        } else if (platformEntity.getPlatformVersion().equals(Constants.VIDEO_VERSION.HK_OPEN8200.id)) {
            syncHikOpen8200Video(platformEntity, orgCode);
        }
    }

    private void syncHikOpen8200Video(VideoPlatformEntity platformEntity, String orgCode) {
        List<DataOrg> orgList;
        List<DataCamera> cameraList;
        if (StringUtils.isEmpty(orgCode)) {
            orgList = open8200Service.getAllOrgs(platformEntity);
            cameraList = open8200Service.getAllCameras(platformEntity);
        } else {
            orgList = open8200Service.getSubOrgs(platformEntity, orgCode);
            cameraList = open8200Service.getSubCameras(platformEntity, orgCode);
        }
        if (orgList != null && orgList.size() > 0) {
            //更新机构
            for (DataOrg org : orgList) {
                VideoOrgEntity orgEntity = videoOrgRepository.getByOrgSn(org.getIndexCode());
                if (orgEntity == null) {
                    orgEntity = new VideoOrgEntity();
                }
                orgEntity.setOrgSn(org.getIndexCode());
                orgEntity.setParentSn(org.getParentIndexCode());
                orgEntity.setOrgName(org.getName());
                orgEntity.setPlatformId(platformEntity.getId());
                videoOrgRepository.save(orgEntity);
            }
        }
        if (cameraList != null && cameraList.size() > 0) {
            //更新摄像头
            for (DataCamera camera : cameraList) {
                VideoCameraEntity cameraEntity = videoCameraRepository.getByCameraSn(camera.getIndexCode());
                if (cameraEntity == null) {
                    cameraEntity = new VideoCameraEntity();
                }
                cameraEntity.setCameraSn(camera.getIndexCode());
                cameraEntity.setCameraName(camera.getName());
                cameraEntity.setOrgSn(camera.getParentIndexCode());
                cameraEntity.setChannelNum(camera.getChanNum());
                int recordLocation = 0;
                if (!StringUtils.isEmpty(camera.getRecLocation())) {
                    try {
                        recordLocation = Integer.parseInt(camera.getRecLocation());
                    } catch (Exception ex) {
                        logger.error("parseInt err: {}", ex.getMessage());
                    }
                }
                cameraEntity.setRecordLocation(recordLocation);
                cameraEntity.setIp(platformEntity.getIp());
                cameraEntity.setIp2(platformEntity.getIp2());
                cameraEntity.setPort(platformEntity.getPort());
                cameraEntity.setUsername(platformEntity.getUsername());
//                cameraEntity.setPasswd(platformEntity.getPasswd());
                cameraEntity.setCameraType(platformEntity.getPlatformType());
                cameraEntity.setCameraVersion(platformEntity.getPlatformVersion());
                videoCameraRepository.save(cameraEntity);
            }
        }
    }

    private void syncHikArtemisVideo(VideoPlatformEntity platformEntity, String orgCode) {
        List<RegionInfo> regionList;
        List<CameraInfo> cameraList;
        if (StringUtils.isEmpty(orgCode)) {
            regionList = hikArtemisService.getAllRegions();
            cameraList = hikArtemisService.getAllCameras();
        } else {
            regionList = hikArtemisService.getSubRegions(orgCode);
            cameraList = hikArtemisService.getSubCameras(orgCode);
        }
        if (regionList != null && regionList.size() > 0) {
            //更新机构
            for (RegionInfo regionInfo : regionList) {
                VideoOrgEntity orgEntity = videoOrgRepository.getByOrgSn(regionInfo.getIndexCode());
                if (orgEntity == null) {
                    orgEntity = new VideoOrgEntity();
                }
                orgEntity.setOrgSn(regionInfo.getIndexCode());
                orgEntity.setParentSn(regionInfo.getParentIndexCode());
                orgEntity.setOrgName(regionInfo.getName());
                orgEntity.setPlatformId(platformEntity.getId());
                videoOrgRepository.save(orgEntity);
            }
        }
        if (cameraList != null && cameraList.size() > 0) {
            //更新摄像头
            for (CameraInfo cameraInfo : cameraList) {
                VideoCameraEntity cameraEntity = videoCameraRepository.getByCameraSn(cameraInfo.getCameraIndexCode());
                if (cameraEntity == null) {
                    cameraEntity = new VideoCameraEntity();
                }
                cameraEntity.setCameraSn(cameraInfo.getCameraIndexCode());
                cameraEntity.setCameraName(cameraInfo.getName() != null ? cameraInfo.getName() : cameraInfo.getCameraName());
                cameraEntity.setOrgSn(cameraInfo.getUnitIndexCode() != null ? cameraInfo.getUnitIndexCode() : cameraInfo.getRegionIndexCode());
                cameraEntity.setChannelNum(cameraInfo.getChanNum());
                int recordLocation = 1;
                if (!StringUtils.isEmpty(cameraInfo.getRecordLocation())) {
                    if (cameraInfo.getRecordLocation().equals("0")) {
                        recordLocation = 0;
                    }
                }
                cameraEntity.setRecordLocation(recordLocation);
                cameraEntity.setIp(platformEntity.getIp());
                cameraEntity.setIp2(platformEntity.getIp2());
                cameraEntity.setPort(platformEntity.getPort());
                cameraEntity.setUsername(platformEntity.getUsername());
//                cameraEntity.setPasswd(platformEntity.getPasswd());
                cameraEntity.setCameraType(platformEntity.getPlatformType());
                cameraEntity.setCameraVersion(platformEntity.getPlatformVersion());
                videoCameraRepository.save(cameraEntity);
            }
        }
    }

    private void updateVideoRes(VideoRes res, VideoPlatformEntity platformEntity) {
        if (res.getResType().equals(Constants.VIDEO_RES_TYPE.ORG.id)) {
            //更新机构
            VideoOrgEntity orgEntity = videoOrgRepository.getByOrgSn(res.getcResCode());
            if (orgEntity == null) {
                orgEntity = new VideoOrgEntity();
            }
            orgEntity.setOrgSn(res.getcResCode());
            orgEntity.setParentSn(res.getcOrgCode());
            orgEntity.setOrgName(res.getcResName());
            orgEntity.setPlatformId(platformEntity.getId());
            videoOrgRepository.save(orgEntity);
        } else if (res.getResType().equals(Constants.VIDEO_RES_TYPE.CAMERA.id)) {
            //更新摄像头
            VideoCameraEntity cameraEntity = videoCameraRepository.getByCameraSn(res.getcResCode());
            if (cameraEntity == null) {
                cameraEntity = new VideoCameraEntity();
            }
            cameraEntity.setCameraSn(res.getcResCode());
            cameraEntity.setCameraName(res.getcResName());
            cameraEntity.setOrgSn(res.getcOrgCode());
            cameraEntity.setIp(platformEntity.getIp());
            cameraEntity.setIp2(platformEntity.getIp2());
            cameraEntity.setPort(platformEntity.getPort());
            cameraEntity.setUsername(platformEntity.getUsername());
            cameraEntity.setPasswd(platformEntity.getPasswd());
            cameraEntity.setCameraType(platformEntity.getPlatformType());
            cameraEntity.setCameraVersion(platformEntity.getPlatformVersion());
            videoCameraRepository.save(cameraEntity);
        }
    }
}
